<?php
class Aregister {
	public static function main($funcode) {
		sview::verify();
		switch ($funcode) {
			case 1:
				self::__dosignin();
				break;
			case 2:
				self::__dochecks();
				break;
			case 3:
				self::__domailsend();
				break;
			default:
				sview::$view->assign_error(404, lang::text(404));
		}
	}
	
	/**
	 * 发送邮件资料信息
	 * @param view_abstract $view
	 * @param int $userid
	 * @param string $email
	 * @param string $auto
	 */
	private static function __mailsend($view, $userid, $email, $auto) {
		$vcode= api_vcode::setmailcode($email, 1, $userid);
		$skey = sprintf('%s;%s;%d', $vcode, $email, $auto);
		$skey = xxtea::encrypt($skey);
		$link = router::get_url('register', 'emailcheck').'?s='.urlencode($skey);
		$link = router::get_base_url().$link;
		//获取发送的模板信息
		$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
		$data = api_mailer::tpl_compiler(array('nickname'=>$user['nickname'], 'url'=>$link), 50004);
		if (!$data) {
			$view->assign_error(80009, lang::text(80009));
		}
		api_mailer::send($data['title'], $data['tpl'], $email);
	}
	
	//会员注册1-2-1
	private static function __dosignin() {
		$view    = view::getinst('apijson');
		$email   = trim(sview::$ctrl['account']);
		$loginpw = trim(sview::$ctrl['loginpw']);
		$nick    = trim(sview::$ctrl['nickname']);
		if (!verify::is_email($email)) {
			sview::$view->assign_error(80018, lang::text(80018));
		}
		if (!verify::is_nick($nick)) {
			sview::$view->assign_error(80002, lang::text(80002));
		}
		$nick    = htmlspecialchars($nick);
		$nick    = stripslashes($nick);
		//验证屏蔽关键词
		$lang    = lang::load('filter.php');
		$res     = api_im::filter(2, $nick);
		if ($res && $res['ResultContent'] == 1) {
			sview::$view->assign_error(80002, lang::replace(80088, $lang['nickname']));
		}
		//验证注册的昵称是否重复
		$userid  = api_nickuin::exist($nick);
		if ($userid > 0) {
			sview::$view->assign_error(80033, lang::text(80033));
		}
		//调用PSocial看看注册是否合法
		$res = api_uc::register($email, $loginpw, $nick);
		if ($res['result'] != '000') {
			sview::$view->assign_error(80018, $res['message']);
		}
		//更新同步信息到本地的业务信息
		$user  = api_uc::sync_user($res['value'], $loginpw);
		//生成下一步的验证签名
		$acl     = acl::getinst('vedio');
		$loginpw = $acl->crypt_loginpw($loginpw);
		$acl->set_loginpw($user['userid'], $loginpw, 0);
		$luser = array('id'=>(int)$user['userid'], 'au'=>0, 'sg'=>0, 'st'=>substr(config::$nowtime, 5));
		//活动发星币
		Amain::onesignal($user['userid']);
		api_activity::sendcoin($user);
		$anchor= model::load('live/anchor')->get_one($user['userid']);
		$roler = empty($anchor)? 0:1;
		$data  = array('signature'=>$acl->setcrypt($luser), 'userid'=>$user['userid'], 'roler'=>$roler);
		//发送验证邮件资料信息
		self::__mailsend(sview::$view, $user['userid'], $email, 0);
		sview::$view->assign_by_ref('datasets', $data);
		sview::$view->display();
	}
	
	//会员注册验证1-2-2
	private static function __dochecks() {
		sview::$ctrl['vtype'] = intval(sview::$ctrl['vtype']);
		sview::$ctrl['vdata'] = trim(sview::$ctrl['vdata']);
		$flag = 0;
		switch (sview::$ctrl['vtype']) {
			case 1: //验证昵称是否被使用
				if (!verify::is_nick(sview::$ctrl['vdata'])) {
					sview::$view->assign_error(80002, lang::text(80002));
				}
				$userid    = 0;
				$acl       = acl::getinst('vedio');
				$signature = $_SERVER['HTTP_SIGNATURE'];
				if ($signature[0] != '*' && $acl->decrypt($signature)) {
					$userid= $acl->user['id'];
				}
				$resid  = api_nickuin::exist(sview::$ctrl['vdata']);
				if ($resid > 0 && $resid != $userid) {//昵称已经被使用了
					$flag = 1;
				}
				break;
			case 2: //验证邮箱是否被注册
				if (!verify::is_email(sview::$ctrl['vdata'])) {
					sview::$view->assign_error(80018, lang::text(80018));
				}
				$res = api_uc::checkemail(sview::$ctrl['vdata']);
				if ($res['result'] != '000') {
					$flag = 1;
				}
				break;
		}
		sview::$view->assign('datasets', array('flag'=>$flag));
		sview::$view->display();
	}
	
	//重新发送邮箱验证1-2-3
	private static function __domailsend() {
		$acl = acl::getinst('vedio');
		$jump= '';
		if ($acl->user) {
			$userid = $acl->user['id'];
			$user   = model::load('live/userinfo')->set_devision($userid)->get_one($userid);
			if ($user['emailverify'] != 1 && verify::is_email($user['email'])) {
				$mjump  = new mailjump();
				$jump   = $mjump->jump($user['email']);
				$queue  = sprintf('ems_%d', $userid);
				$nums   = (int)cache::getinst()->get($queue);
				if ($nums < 3) {
					$auto   = isset($acl->user['au'])? intval($acl->user['au']):0;
					api_mailer::__mailregistersend(sview::$view, $userid, $user['email'], $auto);
					$expire = strtotime(date('Y-m-d'.' 23:59:59')) - config::$nowtime;
					cache::getinst()->set($queue, $nums+1, $expire);
				}
			}
		}
		sview::$view->assign('datasets', array('mailsite'=>$jump));
		sview::$view->display();
	}
}